﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.commands {
    import reprise.utils.*;
    import flash.utils.*;
    import reprise.data.collection.*;

    public class TimeCommandExecutor {

        protected var m_nextKeyIndex:Number;
        protected var m_commands:HashMap;

        protected static var g_instance:TimeCommandExecutor;

        public function TimeCommandExecutor(){
            m_commands = new HashMap();
            m_nextKeyIndex = 0;
        }
        protected function wrapperForName(_arg1:String):Object{
            return (m_commands.objectForKey(_arg1));
        }
        protected function executeWrappedCommand(_arg1:Object):void{
            _arg1.command.execute();
            if (_arg1.oneOff){
                removeCommand(_arg1.command);
            };
        }
        public function removeCommand(_arg1:ICommand):void{
            var _local2:Object = wrapperForCommand(_arg1);
            if (!_local2){
                return;
            };
            clearInterval(_local2.interval);
            m_commands.removeObject(_local2);
        }
        public function delayCommand(_arg1:ICommand, _arg2:Number=0):void{
            if (containsCommand(_arg1)){
                return;
            };
            if ((((_arg2 <= 0)) || (isNaN(_arg2)))){
                _arg1.execute();
                return;
            };
            var _local3:Object = {};
            _local3.command = _arg1;
            _local3.oneOff = true;
            _local3.interval = setInterval(ProxyFunction.create(this, executeWrappedCommand, _local3), _arg2);
            m_commands.setObjectForKey(_local3, getNextKey());
        }
        public function addCommand(_arg1:ICommand, _arg2:Number):void{
            addCommandWithName(_arg1, getNextKey(), _arg2);
        }
        public function containsCommand(_arg1:ICommand):Boolean{
            return (!((wrapperForCommand(_arg1) == null)));
        }
        protected function wrapperForCommand(_arg1:ICommand):Object{
            var _local3:Object;
            var _local4:String;
            var _local2:Object = m_commands.toObject();
            for (_local4 in _local2) {
                _local3 = _local2[_local4];
                if (_local3.command == _arg1){
                    return (_local3);
                };
            };
            return (null);
        }
        protected function getNextKey():String{
            return (("key" + m_nextKeyIndex++));
        }
        public function resetCommandBySettingNewTime(_arg1:ICommand, _arg2:Number=0):void{
            var _local3:Object = wrapperForCommand(_arg1);
            if ((((((_local3 == null)) || (isNaN(_arg2)))) || ((_arg2 < 1)))){
                return;
            };
            clearInterval(_local3.interval);
            _local3.interval = setInterval(ProxyFunction.create(this, executeWrappedCommand, _local3), _arg2);
        }
        public function addCommandWithName(_arg1:ICommand, _arg2:String, _arg3:Number=0):void{
            if (containsCommand(_arg1)){
                return;
            };
            if (m_commands.containsKey(_arg2)){
                removeCommandWithName(_arg2);
            };
            if ((((_arg3 <= 0)) || (isNaN(_arg3)))){
                _arg1.execute();
                return;
            };
            var _local4:Object = {};
            _local4.command = _arg1;
            _local4.interval = setInterval(ProxyFunction.create(this, executeWrappedCommand, _local4), _arg3);
            m_commands.setObjectForKey(_local4, _arg2);
        }
        public function removeCommandWithName(_arg1:String):void{
            var _local2:Object = wrapperForName(_arg1);
            clearInterval(_local2.interval);
            m_commands.removeObjectForKey(_arg1);
        }

        public static function instance():TimeCommandExecutor{
            if (!g_instance){
                g_instance = new (TimeCommandExecutor);
            };
            return (g_instance);
        }

    }
}//package reprise.commands 
